#!/bin/bash
#SBATCH -J HSR_2406_shi               # 指定作业的名称，方便在作业列表中识别该作业
#SBATCH -p batch                  # 指定作业要提交到的分区，不同分区可能有不同的资源配置和使用限制
#SBATCH -N 1                      # 指定作业所需的节点数量，这里设置为 1 表示只使用一个节点
#SBATCH --ntasks-per-node=16    # 每个节点的任务数
#SBATCH --cpus-per-task=4      # 每个任务的CPU核心数
#SBATCH --mem=64G              # 内存需求
#SBATCH --output=./log/2406_%j.out
#SBATCH --time=10:00:00          # 指定作业的最长运行时间，格式为 小时:分钟:秒，这里设置为
#SBATCH --no-requeue             # 作业因系统原因失败，它就不会被自动重新提交执行。
# 加载所需的软件模块

module purge
module load anaconda3
module load lib/intel2019mkl
module load mpi/openmpi-4.1.8-gcc13-ib
module load libs/netcdf/4.7.4
module load libs/hdf5/1.12.2
conda activate py311

export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK
export OMP_PLACES=cores
export OMP_PROC_BIND=close
export PYTHONPATH="/home/shiyongxiang/coda_hsr:$PYTHONPATH"


# 打印环境信息
echo "=== CODA_HSR 作业开始 ==="
echo "时间: $(date)"
echo "作业ID: $SLURM_JOB_ID"
echo "节点: $(hostname)"
echo "MPI进程数: $SLURM_NTASKS"
echo "OMP线程数: $OMP_NUM_THREADS"
echo "MPI实现: $(which mpirun)"
echo "模块列表:"

# 根据传入的参数决定执行哪个步骤
# 如果没有提供参数，则显示帮助信息
if [ $# -eq 0 ]; then
    echo "Usage: $0 [1-9]+"
    echo "Example: $0 456 - Run steps 4, 5, and 6"
    echo "Example: $0 234 - Run steps 2, 3, and 4"
    exit 1
fi

# 遍历输入参数中的每个字符并执行对应的步骤
for step in $(echo $1 | grep -o .); do
    case $step in
        1)
            echo "Running step 1: 1.event.collect.py"

            base=P050
            python 1.event.collect.py -base $base -line 2406  -date 2406 -amp 30  -CHN 0 -debug -outfile ./data/2406.HSR/events/$base.2406.Z.events.h5 -fs 1.0 -fe 40.0
            # python 1.event.collect.py -base P192 -line 2406  -date 2406 -abn  -CHN 0 -debug -fs 1.0 -fe 5.0 -L 3600
            ;;
        2)
            echo "Running step 2: 2.event.info.analysis.py"

            base=P050
            # python 2.event.info.analysis.py -base DZ155
            python 2.event.info.analysis.py -input data/2406.HSR/events/$base.2406.Z.events.h5 -debug

            # python scripts/2A.velocity.statistic.py -input data/2406.HSR/events/P192.2406.Z.events.h5 -output ./figures/2.eventsInfo/P192.HSR.Vel.png
            ;;
        3)
            echo "Running step 3: 3.select.py"

            # cmd="python 3.select.py -date 2406 -base P192 -line P250S -debug -amp 30 -input ./data/2406.HSR/events/P192.2406.H.events.h5"
            # $cmd -emarker P192.LineP250S.S2N.H -S2N 
            # $cmd -emarker P192.LineP250S.N2S.H -N2S 

            cmd="python 3.site.effect.analysis.py -raw -date 2406 -debug"
            $cmd -emarker Z.RC  -input data/2406.HSR/events/P192.2406.Z.events.h5
            ;;
        4)
            echo "Running step 4: 4.corr.py"
            # 进行互相关
            # cmd="python 4.corr.py -date 2406 -base P264 -line P264S -L 40 -Tpos S"
            # $cmd -input data/2406.HSR/events/P349.2406.Z.events.h5 -debug
            # cmd="python 4.corr.py -date 2406 -base P264 -line P264S -L 40 -Tpos N"
            # $cmd -input data/2406.HSR/events/P349.2406.Z.events.h5 -debug

            # cmd="python 4.corr.py -date 2406 -base P349 -line P264S -L 40 -Tpos S"
            # $cmd -input data/2406.HSR/events/P349.2406.Z.events.h5 -debug
            
            cmd="python 4.corr.py -date 2406 -base P192 -line None -L 40 -Tpos S"
            $cmd -input data/2406.HSR/events/P192.2406.Z.events.h5 -debug
            ;;
        5)
            echo "Running step 5: 5.NEstack.py"
            # 叠加获取参考道
            fs=1
            fe=15
            CHN=Z
            base=P320
            line=P250S
            cmd="python 5.NEstack.py -date 2406 -base $base -line $line -debug -PBH -fs $fs -fe $fe -input data/2406.HSR/events/P192.2406.$CHN.events.$base.Line$line.$CHN.T-20.20.corr.h5"

            # $cmd -emarker P340.LineP250S.$CHN.T-40.40.N2S -eventfile data/2406.HSR/events/event_select.P192.LineP250S.N2S.H.csv 
            # $cmd -emarker P340.LineP250S.$CHN.T-40.40.S2N -eventfile data/2406.HSR/events/event_select.P192.LineP250S.S2N.H.csv
            $cmd -emarker $base.Line$line.$CHN.T-40.40.ALL 
            ;;
        6)
            echo "Running step 6: 6.get.traceSeq.py"
            # 将所有数据拼接，获取trace的时间序列
            
            fs=1.0 
            fe=40.0
            common="mpirun  -np $SLURM_NTASKS -x OMP_NUM_THREADS -x OMP_PLACES -x OMP_PROC_BIND --bind-to core \
                    python 6.get.traceSeq.MPI.py -debug -PBH -fs $fs -fe $fe "

            # inputF=data/2406.HSR/events/P349.2406.Z.corr.P349.LineP264S.T100.140.Tp.S.D123.h5
            # $common -input $inputF -noD3

            # inputF=data/2406.HSR/events/P349.2406.Z.corr.P349.LineP264S.T100.140.Tp.S.D123.h5
            # $common -input $inputF -noD1 -noD3

            # inputF=data/2406.HSR/events/P349.2406.Z.corr.P349.LineP264S.T100.140.Tp.S.D123.h5
            # $common -input $inputF -noD2 -noD3

            # inputF=data/2406.HSR/events/P349.2406.Z.corr.P264.LineP264S.T100.140.Tp.N.D123.h5
            # $common -input $inputF

            # for inputF in $(ls data/2406.HSR/events/P050.2406.Z.corr/*.Tp.*.D123.h5); do
            for inputF in $(ls data/2406.HSR/events/P192.2406.Z.corr/*corr.E*.D123.h5); do
                echo "Processing $inputF"
                $common -input $inputF
            done
            ;;
        7)
            echo "Running step 7: 7.pick.Tshift.of.NSstack.py"

            N=25
            
            fs=4.0
            fe=7.0
            inputF=data/2406.HSR/events/P192.2406.Z.corr.traces/P192.2406.Z.traces.P264.LineNONE.T100.140.Tp.N.D123.NONE.F1.0.40.0.PBH.D123.h5
            python 7.pick.Tshift.of.NSstack.py -fs $fs -fe $fe -nstack $N -input $inputF -debug
            ;;
        8)
            echo "Running step 8: 8.comp.tshift.py"
            # 绘制对比图
            line=P250S
            file="data/2406.HSR/traceSeq/P264.Line$line.Z.RC.T-20.20.ALL/*.N.50.h5"
            python 8.merge.dtshift.py -date 2406 -line $line -input "$file" -emarker T-20.20.Z.RC.N50 -dt 1

            file="data/2406.HSR/traceSeq/P264.LineP250S.N.T-20.20.ALL/*.N.50.h5"
            python 8.merge.dtshift.py -date 2406 -line P250S -input "$file" -emarker T-20.20.N.N50 -dt 1

            ;;
        9)
            echo "Running step 9: 9.freq.analysis.py"
            # 配图
            fs=1.0
            fe=3.5
            N=50
            
            for CHN in H Z N E; do
                file="data/2406.HSR/traceSeq/P264.LineP250S.$CHN.T-20.20.ALL/dt.P264.LineP250S.$CHN.T-20.20.ALL.P250S.TO0.0.F$fs.$fe.PBH.dt.P250S.N.$N.h5"
                emarker="2406.ALL.$CHN.F$fs.$fe.PBH.N$N"
                python 9.freq.analysis.py -date 2406 -file $file -emarker $emarker
            done
            ;;
        A)
            echo "Running step A: 绘图"

            # for N in 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26; do
            #     python scripts/6C.get.disp.py -input "data/2406.HSR/events/P192.2406.Z.corr.traces/*.P$N*.Tp.*.*.h5" -emarker P${N}x.D123.Tp.NS
            # done

            # for N in 05 06 07 08 09 10 11; do
            #     python scripts/6C.get.disp.py -input "data/2406.HSR/events/P050.2406.Z.corr.traces/*.P$N*.Tp.*.*.h5" -emarker P${N}x.D123.Tp.NS
            # done
            # python scripts/6C.get.disp.py -input "data/2406.HSR/events/P050.2406.Z.corr.traces/*.P06*.Tp.*.*.h5" -emarker P06x.D123.Tp.NS
            # python scripts/6C.get.disp.py -input "data/2406.HSR/events/P349.2406.Z.corr.traces/*.P34*.Tp.*.*.h5" -emarker P34x.D123.Tp.NS
            # python scripts/6C.get.disp.py -input "data/2406.HSR/events/P192.2406.Z.corr.traces/*.P*.Tp.*.*.h5" -emarker Pxxx.P192.D123.Tp.NS
            # python scripts/6C.get.disp.py -input "data/2406.HSR/events/P050.2406.Z.corr.traces/*.P*.Tp.*.*.h5" -emarker Pxxx.P050.D123.Tp.NS
            # python scripts/6C.get.disp.py -input "data/2406.HSR/events/P349.2406.Z.corr.traces/*.P*.Tp.*.*.h5" -emarker Pxxx.P349.D123.Tp.NS

            # for N in 27 28 29 30 31 32 33 34; do
            #     python scripts/6C.get.disp.py -input "data/2406.HSR/events/P349.2406.Z.corr.traces/*.P$N*.Tp.*.*.h5" -emarker P${N}x.D123.Tp.NS
            # done
            # python scripts/6C.get.disp.py -input "data/2406.HSR/events/P192.2406.Z.corr.traces/*.P12*.Tp.N.*.h5" 
            
            # python convert_old_format.py -i data/2406.HSR/events/P050.2406.Z.corr.old -o data/2406.HSR/events/P050.2406.Z.corr -b
            # python convert_old_format.py -i  data/2406.HSR/events/P192.2406.Z.corr.old -o data/2406.HSR/events/P192.2406.Z.corr -b
            # python convert_old_format.py -i data/2406.HSR/events/P349.2406.Z.corr.old -o data/2406.HSR/events/P349.2406.Z.corr -b

            # for file in data/2406.HSR/events/P349.2406.Z.traces.P349.LineP264S.T100.140.Tp.S.D123.P264S.F1.0.40.0.PBH*.h5; do
            #     python scripts/6B.get.traceSeq.ref.py -input $file
            # done
            # python scripts/6D.get.ref.all.py -input "data/2406.HSR/events/*.2406.Z.corr.traces/*Tp.*.*.h5"
            # python scripts/6D.get.ref.all.py -input "data/2406.HSR/events/P192.2406.Z.corr.traces/*traces.P20*Tp.S.*.h5" -emarker P20x.D123.Tp.S
            # python scripts/6D.get.ref.all.py -input "data/2406.HSR/events/P192.2406.Z.corr.traces/*traces.P20*Tp.N.*.h5" -emarker P20x.D123.Tp.N
            # python scripts/6D.get.ref.all.py -input "data/2406.HSR/events/P349.2406.Z.corr.traces/*.P*.Tp.*.*.h5" -emarker Pxxx.P349.D123.Tp.NS
            # python scripts/6D.get.ref.all.py -input "data/2406.HSR/events/P349.2406.Z.corr.traces/*.P34[56789]*.Tp.N.*.h5" -emarker P34x.D123.Tp.N
            python scripts/6D.get.ref.all.py -input "data/2406.HSR/events/P349.2406.Z.corr.traces/*.P34[56789]*.Tp.S.*.h5" -emarker P34x.D123.Tp.S
            # python scripts/6D.get.ref.all.py -input "data/2406.HSR/events/P349.2406.Z.traces.P264.LineP264S.T100.140.Tp.N.D123.P264S.F1.0.40.0.PBH.D123.h5"
            # python scripts/6D.get.ref.all.py -input "data/2406.HSR/events/P349.2406.Z.corr.traces/*traces.P34*.*Tp.S.*.h5"
            # python scripts/6D.get.ref.all.py -input "data/2406.HSR/events/P050.2406.Z.corr.traces/*.P08*.Tp.S.*.h5" -emarker P08X.D123.Tp.S
            
            # python scripts/6E.extrapolation.Exx.py -input "data/2406.HSR/events/P192.2406.Z.corr.traces/P192.2406.Z.traces.E0[01]*.Tp.S*.h5"
            # python scripts/6E.extrapolation.Exx.py -input "data/2406.HSR/events/P192.2406.Z.corr.traces/P192.2406.Z.traces.E0[01]*.Tp.N*.h5"
            # python scripts/6A.get.traceSeq.plot.py -input data/2406.HSR/traceSeq/P264.LineP250S.Z.RC.T-20.20.ALL/traces.P264.LineP250S.Z.RC.T-20.20.ALL.P250S.TO0.0.F1.0.40.0.PBH.h5

            # python scripts/7A.pick.Tshift.plot.py -input data/2406.HSR/events/P192.2406.Z.corr.traces.dt/P192.2406.Z.traces.P264.LineNONE.T100.140.Tp.N.D123.NONE.F1.0.40.0.PBH.D123.F04.0.07.0.N.025.D123.h5


            # for file in data/2406.HSR/traceSeq/P264.LineP264S.Z.RC.T-20.20.ALL/dt*.h5; do
            #     python scripts/7B.dv.v.plot.py -input $file -vref 3000
            # done
            # python scripts/7B.dv.v.plot.py -input data/2406.HSR/traceSeq/P264.LineP264S.Z.RC.T-20.20.ALL/dt.P264.LineP264S.Z.RC.T-20.20.ALL.P264S.TO0.0.F1.0.40.0.PBH.P264S.F06.0.07.0.N.50.h5 -vref 3000
            # python scripts/7C.dv.vs.st.py -input data/2406.HSR/traceSeq/P264.LineP264S.Z.RC.T-20.20.ALL/dvv.P264S.F06.0.07.0.N.50.V3000.0.h5
            # python scripts/7C.dv.vs.st.py -input data/2406.HSR/traceSeq/P264.LineP264S.Z.RC.T-20.20.ALL/dvv.P264S.F09.0.11.0.N.50.V3000.0.h5
            # python scripts/7C.dv.vs.st.py -input data/2406.HSR/traceSeq/P264.LineP264S.Z.RC.T-20.20.ALL/dvv.P264S.F12.0.14.0.N.50.V3000.0.h5

            # python scripts/7B.dv.v.plot.py -input data/2406.HSR/traceSeq/P264.LineP264S.Z.RC.T-20.20.ALL/dt.P264.LineP264S.Z.RC.T-20.20.ALL.P264S.TO0.0.F1.0.40.0.PBH.P264S.F09.0.11.0.N.50.h5 -vref 3000
            # python scripts/7B.dv.v.plot.py -input data/2406.HSR/traceSeq/P264.LineP264S.Z.RC.T-20.20.ALL/dt.P264.LineP264S.Z.RC.T-20.20.ALL.P264S.TO0.0.F1.0.40.0.PBH.P264S.F12.0.14.0.N.50.h5 -vref 3000

            # python scripts/8A.merge.dtshift.py -input data/2406.HSR/T-20.20.Z.N50.P250S.RES.001H.h5 
            # python scripts/8B.merge.dtshift.py -input data/2406.HSR/T-20.20.Z.RC.N50.$line.RES.001H.h5 -keyN 1
            # python scripts/8C.merge.dtshift.py -input data/2406.HSR/T-20.20.Z.RC.N50.$line.RES.001H.h5 -keyN 1
            ;;
        *)
            echo "Invalid step: $step"
            echo "Valid steps are 1-9"
            exit 1
            ;;
    esac
    
done